package com.xunua.mymycsjsdktest;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;

import java.util.List;

public class NativeExpressActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String ImgCodeId="901121253";//图文
    private static final String VideoCodeId="901121134";//视频

    private EditText et_widht,et_height;
    private Button btn_img,btn_video;
    private TTAdNative adNative;
    private FrameLayout frameLayout;
    private TTNativeExpressAd ttNativeExpressAd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_native_express);
        frameLayout=findViewById(R.id.express_container);
        et_widht=findViewById(R.id.express_width);
        et_height=findViewById(R.id.express_height);
        btn_img=findViewById(R.id.btn_express_load);
        btn_video=findViewById(R.id.btn_express_load_video);
        btn_img.setOnClickListener(this);
        btn_video.setOnClickListener(this);

        TTAdManager adManager = TTAdSdk.getAdManager();
        adNative = adManager.createAdNative(NativeExpressActivity.this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_express_load:
                //图文广告
                if (et_widht.getText().toString().trim().isEmpty()||et_height.getText().toString().trim().isEmpty())
                    return;

                initAdExpress(ImgCodeId, Float.parseFloat(et_widht.getText().toString().trim()), Float.parseFloat(et_height.getText().toString().trim()));
                break;
            case R.id.btn_express_load_video:
                //视频广告
                if (et_widht.getText().toString().trim().isEmpty()||et_height.getText().toString().trim().isEmpty())
                    return;

                initAdExpress(VideoCodeId, Float.parseFloat(et_widht.getText().toString().trim()), Float.parseFloat(et_height.getText().toString().trim()));
                break;
        }
    }
    private void initAdExpress(String codeId, float expressViewWidth, float expressViewHeight){
        //设置广告参数
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(codeId) //广告位id
                .setSupportDeepLink(true)
                .setAdCount(1) //请求广告数量为1到3条
                .setExpressViewAcceptedSize(expressViewWidth,expressViewHeight) //期望个性化模板广告view的size,单位dp    高度设置为0会自适应
                .setImageAcceptedSize(640,320) //这个参数设置即可，不影响个性化模板广告的size
                .build();
        adNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int i, String s) {
                Toast.makeText(NativeExpressActivity.this, i+"-----------"+s, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
                if (list == null || list.size() == 0) {
                    return;
                }
                ttNativeExpressAd = list.get(0);
                bindAdListener(ttNativeExpressAd);
                ttNativeExpressAd.render();//开始渲染广告
            }
        });
    }

    private void bindAdListener(TTNativeExpressAd ad) {
        ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override
            public void onAdClicked(View view, int i) {
                Toast.makeText(NativeExpressActivity.this, "广告被点击", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdShow(View view, int i) {
                Toast.makeText(NativeExpressActivity.this, "广告展示", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRenderFail(View view, String s, int i) {
                Toast.makeText(NativeExpressActivity.this, "错误", Toast.LENGTH_SHORT).show();
            }

            /**
             *
             * @param view
             * @param v     view的宽
             * @param v1    view的高
             */
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onRenderSuccess(View view, float v, float v1) {
                Toast.makeText(NativeExpressActivity.this, "渲染成功", Toast.LENGTH_SHORT).show();
                //在渲染成功时展示广告，提升体验
                frameLayout.removeAllViews();
                frameLayout.addView(view);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (ttNativeExpressAd != null) {
            ttNativeExpressAd.destroy();
        }
    }
}